home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / netconf / netconf.c < prev    next >
C/C++ Source or Header  |  1996-08-02  |  10KB  |  344 lines

  1. #pragma implementation
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <stdarg.h>
  7. #include <netdb.h>
  8. #include "../fstab/fstab.h"
  9. #include "../dnsconf/dnsconf.h"
  10. #include "../mailconf/mailconf.h"
  11. #include "../userconf/userconf.h"
  12. #include "../askrunlevel/askrunlevel.h"
  13. #include "../uucp/uucp.h"
  14. #include "netconf.h"
  15. #include "../xconf/xconf.h"
  16. #include "netconf.m"
  17.  
  18. static NETCONF_HELP_FILE help_others ("others");
  19. static NETCONF_HELP_FILE help_netconf ("netconf");
  20.  
  21. /*
  22.     Bring the system in sync with its configuration.
  23. */
  24. void netconf_update()
  25. {
  26.     if (perm_rootaccess(MSG_U(P_PERMUPDATE
  27.         ,"update the status of the system"))){
  28.         if (!simul_ison()) net_introlog ("netconf --update");
  29.         netconf_runlevel (-1);
  30.     }
  31. }
  32. /*
  33.     Check what have to be done to bring the system in sync
  34.     with its configuration.
  35. */
  36. void netconf_status()
  37. {
  38.     simul_init (stdout);
  39.     printf (MSG_U(X_LIST
  40.         ,"List of things requiered to activate current configuration\n"));
  41.     netconf_update();
  42. }
  43.  
  44. void netconf_edit()
  45. {
  46.     /* #Specification: netconf / principal
  47.         netconf use a large flat menu to direct the user to
  48.         the proper configuration dialogs. The menu is splitted
  49.         in several sections.
  50.  
  51.         #
  52.         Basic informations.
  53.         Client related configurations
  54.         Server related configurations.
  55.         Firewalling.
  56.         #
  57.     */
  58.     int choice=0;
  59.     while (1){
  60.         static const char *your_system = MSG_U(M_INFOR,"information about your system");
  61.         static const char *other_hosts = MSG_U(M_HOSTS,"information about other hosts");
  62.         static const char *other_networks = MSG_U(M_NETWORKS,"information about other networks");
  63.         static const char *routing_tables = MSG_U(M_ROUTING,"routing and gateways");
  64.         static const char *resolv= MSG_U(M_RESOLV,"Name server specification (DNS)");
  65.         static const char *nis= MSG_U(M_NIS,"Network Information System (NIS)");
  66.         static const char *order= MSG_U(M_ORDER,"Search path for host name");
  67.         static const char *ipx= MSG_U(M_IPXCONF,"IPX interface setup");
  68.         static const char *exports= MSG_U(M_EXPORTS,"exported file-systems (NFS)");
  69.         static const char *dns= MSG_U(M_DNS,"Domain Name Server (DNS)");
  70.         static const char *sendmail = MSG_U(M_MAIL,"Mail delivery system (sendmail)");
  71.         static const char *uucp     = MSG_U(M_UUCP,"UUCP (Unix to unix copy)");
  72.         static const char *alias    = MSG_U(M_ALIAS,"IP aliases for virtual hosts");
  73.         static const char *firewallc = MSG_U(M_FWDEF,"firewalling defaults (packet filtering)");
  74.         static const char *firewallf = MSG_U(M_FORWARD,"forward firewalling");
  75.         static const char *firewallb = MSG_U(M_BLOCK,"blocking firewalling");
  76.         static const char *firewallo = MSG_U(M_OUTPUT,"outputing firewalling");
  77.         static const char *firewalla = MSG_U(M_ACCT,"packets accounting");
  78.         static const char *ppp = MSG_U(M_PPP,"ppp or slip dialout");
  79.         static const char *rarp = MSG_U(M_RARP,"RARP server");
  80.         static const char *htmlaccess = MSG_U(M_HTMLACCES,"linuxconf network access");
  81.         static const char *isolated = MSG_U(M_ISOL,"Isolated/simple network from scratch");
  82.         static const char *menuopt[]={
  83.             MSG_U(M_EDIT,"Edit"),        your_system,
  84.             " ",        other_hosts,
  85.             " ",        other_networks,
  86.  
  87.             "-",        MSG_U(T_CLIENTTASKS,"Client tasks"),
  88.             MSG_R(M_CONFIG),    resolv,
  89.             " ",        routing_tables,
  90.             " ",        order,
  91.             " ",        nis,
  92.             " ",        ipx,
  93.             " ",        ppp,
  94.  
  95.             "-",        MSG_U(T_SERVER,"Server tasks"),
  96.             MSG_R(M_CONFIG),    exports,
  97.             " ",        dns,
  98.             " ",        sendmail,
  99.             " ",        uucp,
  100.             " ",        alias,
  101.             "-",        MSG_U(T_BOOT,"Boot services"),
  102.             " ",        rarp,
  103.  
  104.             "-",        MSG_U(T_FIREWALL,"Firewalling & packets Accounting"),
  105.             MSG_R(M_CONFIG),    firewallc,
  106.             " ",        firewallb,
  107.             " ",        firewallf,
  108.             " ",        firewallo,
  109.             " ",        firewalla,
  110.  
  111.             "-",        "",
  112.             MSG_R(M_CONFIG),    htmlaccess,
  113.             MSG_U(M_INSTALL,"Install"),    isolated,
  114.             NULL
  115.         };
  116.         MENU_STATUS code = xconf_menu (
  117.             MSG_U(T_NETWORK,"Network configurator")
  118.             ,MSG_U(I_NETWORK,"This package allows you to configure from\n"
  119.              "scratch a TCP/IP network using ethernet\n"
  120.              "and serial cable (modem)")
  121.             ,help_netconf
  122.             ,menuopt,choice);
  123.         if (code == MENU_QUIT || code == MENU_ESCAPE){
  124.             break;
  125.         }else{
  126.             const char *key = menuopt[choice*2+1];
  127.             if (key == isolated){
  128.                 simple_install();
  129.             }else if (key == your_system){
  130.                 netconf_edithost();
  131.             }else if (key == other_hosts){
  132.                 netconf_edithosts();
  133.             }else if (key == other_networks){
  134.                 netconf_editnet();
  135.             }else if (key == routing_tables){
  136.                 netconf_editroutes();
  137.             }else if (key == nis){
  138.                 netconf_editnis();
  139.             }else if (key == resolv){
  140.                 dnsconf_editresolv();
  141.             }else if (key == order){
  142.                 dnsconf_editorder();
  143.             }else if (key == ipx){
  144.                 ipx_edit();
  145.             }else if (key == exports){
  146.                 exports_edit();
  147.             }else if (key == dns){
  148.                 dnsconf_edit();
  149.             }else if (key == sendmail){
  150.                 mailconf_edit();
  151.             }else if (key == uucp){
  152.                 uucp_edit();
  153.             }else if (key == alias){
  154.                 alias_edit();
  155.             }else if (key == firewallc){
  156.                 firewall_editc();
  157.             }else if (key == firewallf){
  158.                 firewall_editf();
  159.             }else if (key == firewallb){
  160.                 firewall_editb();
  161.             }else if (key == firewalla){
  162.                 firewall_edita();
  163.             }else if (key == firewallo){
  164.                 firewall_edito();
  165.             }else if (key == ppp){
  166.                 ppp_edit();
  167.             }else if (key == rarp){
  168.                 rarp_edit();
  169.             }else if (key == htmlaccess){
  170.                 html_access_edit();
  171.             }
  172.         }
  173.     }
  174. }
  175.  
  176. /*
  177.     Return 0 if nothing had to be done, 1 if it was done, -1 if something
  178.     had to be done but was not done.
  179. */
  180. int netconf_checkupdate()
  181. {
  182.     /* #Specification: configuration / probing for update
  183.         linuxconf can compare the current (running)
  184.         configuration of the machine with the intended
  185.         (what is setup in configuration files) one.
  186.  
  187.         It will compute what has to be done to bring
  188.         the machine in sync with the intended configuraiton.
  189.  
  190.         It will present a summary to the user and let him
  191.         decide if he wants to activate this now or wait later.
  192.  
  193.         One (maybe) unrelated mecanism has been added here.
  194.         The fixperm facility is included in the probing.
  195.         It is expected that with both set of tests, the linux
  196.         machine will be always up and running without
  197.         puzzling bugs for the administrator.
  198.     */
  199.     int ret = -1;
  200.     if (perm_rootaccess(MSG_R(P_PERMUPDATE))){
  201.         simul_init();
  202.         netconf_update();
  203.         ret = simul_prompt();
  204.         if (ret==1){
  205.             netconf_update ();
  206.         }
  207.     }
  208.     return ret;
  209. }
  210.  
  211. static void usage()
  212. {
  213.     xconf_error ("%s\n"
  214.         "netconf --connect site [--fore]\n"
  215.         "netconf --disconnect site\n"
  216.         "netconf --resetfw\n"
  217.         "netconf --runlevel 0|1|2\n"
  218.         "netconf --runlevel local|client|server\n"
  219.         "netconf --update\n"
  220.         "netconf --status\n"
  221.         "\n"
  222.         "%s\n"
  223.         ,MSG_U(E_NETCONF,"Netconf: Invalid arguments\n")
  224.         ,MSG_U(E_NETCONFDEF,"netconf without argument start the interactive mode\n")
  225.         );
  226. }
  227.  
  228. static void netconf_gorun(const char *arg)
  229. {
  230.     /* #Specification: netconf / runlevel option
  231.         if netconf is run with the argument --runlevel, it will
  232.         activate/desactivate all the daemons associated with this
  233.         runlevel. It will also check if running daemons are informed
  234.         of any changes in there configuration files.
  235.     */
  236.     const char *pt = arg;
  237.     int netmode=-1;
  238.     if (strcmp(arg,"local")==0){
  239.         netmode = 0;
  240.     }else if (strcmp(arg,"client")==0){
  241.         netmode = 1;
  242.     }else if (strcmp(arg,"server")==0){
  243.         netmode = 2;
  244.     }else{
  245.         int runlevel = atoi(arg);
  246.         while (*pt != '\0'){
  247.             if (!isdigit(*pt)){
  248.                 runlevel = -1;
  249.                 break;
  250.             }
  251.             pt++;
  252.         }
  253.         if (runlevel > 0 && runlevel < 7){
  254.             static char tb[7]={0
  255.                 ,1    // Maintenance mode
  256.                 ,0    // Text mode only
  257.                 ,2    // Text mode and network (server mode)
  258.                 ,1    // X terminal
  259.                 ,0    // Graphic only
  260.                 ,2    // Graphic and network
  261.             };
  262.             netmode = tb[runlevel];
  263.         }
  264.     }
  265.     if (netmode != -1){
  266.         char cmd[100];
  267.         static char *tb[]={"local","client","server"};
  268.         sprintf (cmd,"echo netconf --runlevel %s: `date`>>%s"
  269.             ,tb[netmode],TMP_NETCONF_LOG);
  270.         system  (cmd);
  271.         netconf_runlevel (netmode);
  272.     }else{
  273.         xconf_error (MSG_U(E_IVLRUNLEVEL
  274.             ,"Invalid runlevel %s for network daemons\n"
  275.              "Values 1,2,3,4,5,6 or local,client,server\n"
  276.              "are valid.\n")
  277.             ,arg);
  278.         dialog_end();
  279.     }
  280. }
  281.  
  282. int netconf_main (int argc, char *argv[])
  283. {
  284.     if (argc == 1){
  285.         netconf_edit();
  286.         netconf_checkupdate();
  287.     }else if (argc == 2){
  288.         char *arg1 = argv[1];
  289.         if (strcmp(arg1,"--update")==0){
  290.             /* #Specification: netconf / option / --update
  291.                 netconf --update is equivalent to run netconf --runlevel
  292.                 with the current runlevel value.
  293.             */
  294.             netconf_update();
  295.         }else if (strcmp(arg1,"--status")==0){
  296.             /* #Specification: netconf / option / --status
  297.                 netconf --status perform a simulation of netconf --update.
  298.                 It simply shows what netconf --update will do to activate
  299.                 the current configuration. Most of time, it prints
  300.                 only a header, since the system is "current".
  301.             */
  302.             netconf_status();
  303.         }else if (strcmp(arg1,"--resetfw")==0){
  304.             /* #Specification: netconf / option / --resetfw
  305.                 netconf --resetfw turn off completly the
  306.                 firewalling configuration. This means that
  307.                 the machine is now wide open (as far as
  308.                 packet filtering goes).
  309.  
  310.                 This only reset the "running" configuration,
  311.                 not the configuration itself.
  312.                 netconf --update will bring the firewall back
  313.                 to its previous state.
  314.             */
  315.             firewall_reset();
  316.         }else{
  317.             usage();
  318.         }
  319.     }else if (argc >= 3){
  320.         char *arg1 = argv[1];
  321.         if (strcmp(arg1,"--connect")==0){
  322.             /* #Specification: netconf / option / --connect
  323.                 netconf --connect establish a PPP or SLIP
  324.                 connection to a preconfigured site.
  325.             */
  326.             ppp_connect (argc-2,argv+2);
  327.         }else if (strcmp(arg1,"--disconnect")==0){
  328.             /* #Specification: netconf / option / --disconnect
  329.                 netconf --disconnect terminate a PPP or SLIP
  330.                 connection.
  331.             */
  332.             ppp_disconnect (argc-2,argv+2);
  333.         }else if (strcmp(arg1,"--runlevel")==0){
  334.             netconf_gorun(argv[2]);
  335.         }else{
  336.             usage();
  337.         }
  338.     }else{
  339.         usage();
  340.     }
  341.     return 0;
  342. }
  343.  
  344.